<template>
    <div id="wgySwiper">
        <!-- ul的宽度等于数组的数量*100%-->
        <ul :style=" {
            width:100 * AloneSwiper.length + '%',
            left:left,
            transition:transition
        } ">
            <li v-for="(item,i) in AloneSwiper" :key="i" :style="{
                    width:100 / AloneSwiper.length + '%'
                }"  @touchstart='touchStart'  @touchmove='touchMove' @touchend='touchEnd'>
        <!-- li的宽度等于100/数组的数量%-->

                <img ref="img" :src="item" alt="" >
            </li>
        </ul>
    </div>
</template>

<script>
let setInt
export default {
    props:['swiperList'],
    data() {
        return {
            left:'-100%',
            transition:'none',
            direction:'left',
            startX:0,
            AloneSwiper:[
                        'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1652123795,1945063222&fm=26&gp=0.jpg',
                        'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fattach.bbs.miui.com%2Fforum%2F201312%2F31%2F111859myvyiivetyftfz2n.jpg&refer=http%3A%2F%2Fattach.bbs.miui.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1613282880&t=9bdabd24fa0f1fd74418536f2043000c',
                        'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fattach.bbs.miui.com%2Fforum%2F201310%2F19%2F235439yh04c010wm0qrk5d.jpg&refer=http%3A%2F%2Fattach.bbs.miui.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1613282880&t=b0bb5c3daa693bb192e5a042706ace03'],

        };
    },
    created() {
        if(this.autoplay !== false){
            setInt = setInterval(()=>{
                this.touchEnd()
            },3000)
        }
       
    },
    mounted() {
        // 如果传递过来数据就使用数据,

        if(this.swiperList){
            this.AloneSwiper = this.swiperList
        }
    },
    methods: {
        touchStart(e) {
            // console.log(e);
            // 获取点击位置的x坐标
            this.startX = e.touches[0].pageX    
            clearInterval(setInt)
            

        },
        touchMove(e) {
            // 获取移动方向
            let moveX = e.touches[0].pageX - this.startX
            if(moveX > 0){
                this.direction = 'right'
            } else if(moveX < 0){
                 this.direction = 'left'
            }
        },
        touchEnd() {
            let timer 

            this.transition = 'left .5s linear'
            clearTimeout(timer)

            if(this.direction == 'left'){
                this.left = '-200%'
                timer = setTimeout(() => {
                    let item = this.AloneSwiper.shift()
                    this.AloneSwiper.push(item)
                    this.transition = 'none'
                    this.left = '-100%'
                },500)
            }else if(this.direction == 'right'){
                 this.left = '0%'
                timer = setTimeout(() => {
                    let item = this.AloneSwiper.pop()
                    this.AloneSwiper.unshift(item)
                    this.transition = 'none'
                    this.left = '-100%'
                },500)
            }
        }
    },
    //组件挂载
    components: {},
    //计算属性
    computed: {},
    //过滤器
    filters: {},
};
</script>

<style lang="scss" scoped>

#wgySwiper{
    width: 100%;
    position: relative;
    ul{
       width:274px;
        overflow: hidden;
        width: 100%;
       
        position: relative;
        li{
            width: 100%;
            height:  100%;
            float: left;
            img{
                width: 100%;
            }
        }
    }
    // #uu{
    //     border-radius: 20% 20% 0 0;
    //     border-bottom: 0.2rem solid #F5F5F5;
    //     position: absolute;
    //     bottom: 0px;
    //     left: 0;
    //     width: 100%;
    //     height: 2.2rem;
    //     background-color: #fff;
    //     display: flex;
    //     justify-content: space-around;
    //     align-items: center;
    //     li{
    //         width: 25%;
    //         text-align: center;
    //         font-size: .36rem;
    //         display: list-item;
    //         text-align: -webkit-match-parent;
    //         img{
    //             width: 0.6rem;
    //             display: block;
    //             margin: 0 auto;
    //         }
    //         p{
    //             text-align: center;
    //             margin-top: 0.2rem;
    //         }
    //     }
    // }
}
</style>
